package util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import util.enumeration.Language;
import util.enumeration.OperatingHours;
import util.enumhelper.SupportedLocale;
import util.localization.WebLocalization;


/**
 *
 * @author Amelia
 */
@FacesConverter(value = "operatingHoursConverter", forClass = util.enumeration.OperatingHours.class)
public class OperatingHoursConverter implements Converter {
    
    private WebLocalization localization;
    private String preferredLocale;
    
    
    public OperatingHoursConverter() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        /* END LOCALIZATION */
    }
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {    

        preferredLocale = localization.getPreferredLocaleFromSession();
        
        if (value == null || value.length() == 0) {
            return null;
        }
         
        // en_US ENGLISH, UNITED STATES
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
            return getOperatingHoursAsObject_EN_US(value);
        }
        // en_SG ENGLISH, SINGAPORE
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
            return getOperatingHoursAsObject_EN_SG(value);
        }
        // ms_MY MALAY, MALAYSIA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
            return getOperatingHoursAsObject_MS_MY(value);
        }

        // zh_CN CHINESE, CHINA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
            return getOperatingHoursAsObject_ZH_CN(value);
        }
        // other language...
        else {
            return getOperatingHoursAsObject_EN_US(value);
        }
    }
    
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        if (value instanceof OperatingHours) {
          OperatingHours operatingHours = (OperatingHours) value;
        
        String localizedOperatingHours = "";
        
        // en_US ENGLISH, UNITED STATES
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
            localizedOperatingHours = getOperatingHours_EN_US(operatingHours);
        }
        // en_SG ENGLISH, SINGAPORE
        else if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
            localizedOperatingHours = getOperatingHours_EN_SG(operatingHours);
        }
        // ms_MY MALAY, MALAYSIA
        else if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
            localizedOperatingHours = getOperatingHours_MS_MY(operatingHours);
        }
        
        // zh_CN CHINESE, CHINA
        else if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
            localizedOperatingHours = getOperatingHours_ZH_CN(operatingHours);
        }
        // other language...
        else {
            localizedOperatingHours = getOperatingHours_EN_US(operatingHours);
        }
         return localizedOperatingHours;
        }
        else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.OperatingHours");
        }
    }
    
    // simplified chinese (zh_CN)
    private static Object getOperatingHoursAsObject_ZH_CN(String value) {
        if(value.equals("早上八点")) return OperatingHours.EightAM;
        else if(value.equals("早上九点")) return OperatingHours.NineAM;
        else if(value.equals("早上十点")) return OperatingHours.TenAM;
        else if(value.equals("早上十一点")) return OperatingHours.ElevenAM;
        else if(value.equals("中午十二点")) return OperatingHours.TwelvePM;
        else if(value.equals("下午一点")) return OperatingHours.OnePM;
        else if(value.equals("下午二点")) return OperatingHours.TwoPM;
        else if(value.equals("下午三点")) return OperatingHours.ThreePM;
        else if(value.equals("下午四点")) return OperatingHours.FourPM;
        else if(value.equals("下午五点")) return OperatingHours.FivePM;
        else if(value.equals("下午六点")) return OperatingHours.SixPM;
        else if(value.equals("晚上七点")) return OperatingHours.SevenPM;
        else if(value.equals("晚上八点")) return OperatingHours.EightPM;
        else if(value.equals("晚上九点")) return OperatingHours.NinePM;
        else if(value.equals("晚上十点")) return OperatingHours.TenPM;
        else if(value.equals("晚上十一点")) return OperatingHours.ElevenPM;
        else if(value.equals("凌晨十二点")) return OperatingHours.TwelveAM;
        else if(value.equals("凌晨一点")) return OperatingHours.OneAM;
        else if(value.equals("凌晨二点")) return OperatingHours.TwoAM;
        else if(value.equals("凌晨三点")) return OperatingHours.ThreeAM;
        else if(value.equals("凌晨四点")) return OperatingHours.FourAM;
        else if(value.equals("凌晨五点")) return OperatingHours.FiveAM;
        else if(value.equals("凌晨六点")) return OperatingHours.SixAM;
        else if(value.equals("凌晨七点")) return OperatingHours.SevenAM;
        else return OperatingHours.OFF;
    }
    
     // default (en_US)
    private static Object getOperatingHoursAsObject_EN_US(String value) {
        if(value.equals("8:00am")) return OperatingHours.EightAM;
        else if(value.equals("9:00am")) return OperatingHours.NineAM;
        else if(value.equals("10:00am")) return OperatingHours.TenAM;
        else if(value.equals("11:00am")) return OperatingHours.ElevenAM;
        else if(value.equals("12:00pm")) return OperatingHours.TwelvePM;
        else if(value.equals("1:00pm")) return OperatingHours.OnePM;
        else if(value.equals("2:00pm")) return OperatingHours.TwoPM;
        else if(value.equals("3:00pm")) return OperatingHours.ThreePM;
        else if(value.equals("4:00pm")) return OperatingHours.FourPM;
        else if(value.equals("5:00pm")) return OperatingHours.FivePM;
        else if(value.equals("6:00pm")) return OperatingHours.SixPM;
        else if(value.equals("7:00pm")) return OperatingHours.SevenPM;
        else if(value.equals("8:00pm")) return OperatingHours.EightPM;
        else if(value.equals("9:00pm")) return OperatingHours.NinePM;
        else if(value.equals("10:00pm")) return OperatingHours.TenPM;
        else if(value.equals("11:00pm")) return OperatingHours.ElevenPM;
        else if(value.equals("12:00am")) return OperatingHours.TwelveAM;
        else if(value.equals("1:00am")) return OperatingHours.OneAM;
        else if(value.equals("2:00am")) return OperatingHours.TwoAM;
        else if(value.equals("3:00am")) return OperatingHours.ThreeAM;
        else if(value.equals("4:00am")) return OperatingHours.FourAM;
        else if(value.equals("5:00am")) return OperatingHours.FiveAM;
        else if(value.equals("6:00am")) return OperatingHours.SixAM;
        else if(value.equals("7:00am")) return OperatingHours.SevenAM;
        else return OperatingHours.OFF;
    }
    
    // singapore english (en_SG)
    private static Object getOperatingHoursAsObject_EN_SG(String value) {
        if(value.equals("8:00am")) return OperatingHours.EightAM;
        else if(value.equals("9:00am")) return OperatingHours.NineAM;
        else if(value.equals("10:00am")) return OperatingHours.TenAM;
        else if(value.equals("11:00am")) return OperatingHours.ElevenAM;
        else if(value.equals("12:00pm")) return OperatingHours.TwelvePM;
        else if(value.equals("1:00pm")) return OperatingHours.OnePM;
        else if(value.equals("2:00pm")) return OperatingHours.TwoPM;
        else if(value.equals("3:00pm")) return OperatingHours.ThreePM;
        else if(value.equals("4:00pm")) return OperatingHours.FourPM;
        else if(value.equals("5:00pm")) return OperatingHours.FivePM;
        else if(value.equals("6:00pm")) return OperatingHours.SixPM;
        else if(value.equals("7:00pm")) return OperatingHours.SevenPM;
        else if(value.equals("8:00pm")) return OperatingHours.EightPM;
        else if(value.equals("9:00pm")) return OperatingHours.NinePM;
        else if(value.equals("10:00pm")) return OperatingHours.TenPM;
        else if(value.equals("11:00pm")) return OperatingHours.ElevenPM;
        else if(value.equals("12:00am")) return OperatingHours.TwelveAM;
        else if(value.equals("1:00am")) return OperatingHours.OneAM;
        else if(value.equals("2:00am")) return OperatingHours.TwoAM;
        else if(value.equals("3:00am")) return OperatingHours.ThreeAM;
        else if(value.equals("4:00am")) return OperatingHours.FourAM;
        else if(value.equals("5:00am")) return OperatingHours.FiveAM;
        else if(value.equals("6:00am")) return OperatingHours.SixAM;
        else if(value.equals("7:00am")) return OperatingHours.SevenAM;
        else return OperatingHours.OFF;
    }
    
     // singapore english (en_SG)
    private static Object getOperatingHoursAsObject_MS_MY(String value) {
        if(value.equals("8:00am")) return OperatingHours.EightAM;
        else if(value.equals("9:00am")) return OperatingHours.NineAM;
        else if(value.equals("10:00am")) return OperatingHours.TenAM;
        else if(value.equals("11:00am")) return OperatingHours.ElevenAM;
        else if(value.equals("12:00pm")) return OperatingHours.TwelvePM;
        else if(value.equals("1:00pm")) return OperatingHours.OnePM;
        else if(value.equals("2:00pm")) return OperatingHours.TwoPM;
        else if(value.equals("3:00pm")) return OperatingHours.ThreePM;
        else if(value.equals("4:00pm")) return OperatingHours.FourPM;
        else if(value.equals("5:00pm")) return OperatingHours.FivePM;
        else if(value.equals("6:00pm")) return OperatingHours.SixPM;
        else if(value.equals("7:00pm")) return OperatingHours.SevenPM;
        else if(value.equals("8:00pm")) return OperatingHours.EightPM;
        else if(value.equals("9:00pm")) return OperatingHours.NinePM;
        else if(value.equals("10:00pm")) return OperatingHours.TenPM;
        else if(value.equals("11:00pm")) return OperatingHours.ElevenPM;
        else if(value.equals("12:00am")) return OperatingHours.TwelveAM;
        else if(value.equals("1:00am")) return OperatingHours.OneAM;
        else if(value.equals("2:00am")) return OperatingHours.TwoAM;
        else if(value.equals("3:00am")) return OperatingHours.ThreeAM;
        else if(value.equals("4:00am")) return OperatingHours.FourAM;
        else if(value.equals("5:00am")) return OperatingHours.FiveAM;
        else if(value.equals("6:00am")) return OperatingHours.SixAM;
        else if(value.equals("7:00am")) return OperatingHours.SevenAM;
        else return OperatingHours.OFF;
    }
    
    

    // simplified chinese (zh_CN)
    private static String getOperatingHours_ZH_CN(OperatingHours operatingHours) {
        String localizedOperatingHours = "";
        
        switch (operatingHours) {
            case EightAM: localizedOperatingHours = "早上八点" ; break;
            case NineAM: localizedOperatingHours = "早上九点"; break;
            case TenAM: localizedOperatingHours = "早上十点"; break;
            case ElevenAM: localizedOperatingHours = "早上十一点"; break;
            case TwelvePM: localizedOperatingHours = "中午十二点"; break;
            case OnePM: localizedOperatingHours = "下午一点"; break;
            case TwoPM: localizedOperatingHours = "下午二点"; break;
            case ThreePM: localizedOperatingHours = "下午三点"; break;
            case FourPM: localizedOperatingHours = "下午四点"; break;
            case FivePM: localizedOperatingHours = "下午五点"; break;
            case SixPM: localizedOperatingHours = "下午六点"; break;
            case SevenPM: localizedOperatingHours = "晚上七点"; break;
            case EightPM: localizedOperatingHours = "晚上八点"; break;
            case NinePM: localizedOperatingHours = "晚上九点"; break;
            case TenPM: localizedOperatingHours = "晚上十点"; break;
            case ElevenPM: localizedOperatingHours = "晚上十一点" ; break;
            case TwelveAM: localizedOperatingHours = "凌晨十二点"; break;
            case OneAM: localizedOperatingHours = "凌晨一点"; break;
            case TwoAM: localizedOperatingHours = "凌晨二点"; break;
            case ThreeAM: localizedOperatingHours = "凌晨三点"; break;
            case FourAM: localizedOperatingHours = "凌晨四点"; break;
            case FiveAM: localizedOperatingHours = "凌晨五点"; break;
            case SixAM: localizedOperatingHours = "凌晨六点"; break;
            case SevenAM: localizedOperatingHours = "凌晨七点"; break;
            case OFF: localizedOperatingHours = "休息日"; break;
         }
        return localizedOperatingHours;
    }
    
    // default (en_US)
    private static String getOperatingHours_EN_US(OperatingHours operatingHours) {
        String localizedOperatingHours = "";
        
        switch (operatingHours) {
            case EightAM: localizedOperatingHours = "8:00am" ; break;
            case NineAM: localizedOperatingHours = "9:00am"; break;
            case TenAM: localizedOperatingHours = "10:00am"; break;
            case ElevenAM: localizedOperatingHours = "11:00am"; break;
            case TwelvePM: localizedOperatingHours = "12:00pm"; break;
            case OnePM: localizedOperatingHours = "1:00pm"; break;
            case TwoPM: localizedOperatingHours = "2:00pm"; break;
            case ThreePM: localizedOperatingHours = "3:00pm"; break;
            case FourPM: localizedOperatingHours = "4:00pm"; break;
            case FivePM: localizedOperatingHours = "5:00pm"; break;
            case SixPM: localizedOperatingHours = "6:00pm"; break;
            case SevenPM: localizedOperatingHours = "7:00pm"; break;
            case EightPM: localizedOperatingHours = "8:00pm"; break;
            case NinePM: localizedOperatingHours = "9:00pm"; break;
            case TenPM: localizedOperatingHours = "10:00pm"; break;
            case ElevenPM: localizedOperatingHours = "11:00pm" ; break;
            case TwelveAM: localizedOperatingHours = "12:00pm"; break;
            case OneAM: localizedOperatingHours = "1:00am"; break;
            case TwoAM: localizedOperatingHours = "2:00am"; break;
            case ThreeAM: localizedOperatingHours = "3:00am"; break;
            case FourAM: localizedOperatingHours = "4:00am"; break;
            case FiveAM: localizedOperatingHours = "5:00am"; break;
            case SixAM: localizedOperatingHours = "6:00am"; break;
            case SevenAM: localizedOperatingHours = "7:00am"; break;
            case OFF: localizedOperatingHours = "OFF DAY"; break;
         }
        return localizedOperatingHours;
    }
    
    // singapore english (en_SG)
    private static String getOperatingHours_EN_SG(OperatingHours operatingHours) {
             String localizedOperatingHours = "";
        
        switch (operatingHours) {
            case EightAM: localizedOperatingHours = "8:00am" ; break;
            case NineAM: localizedOperatingHours = "9:00am"; break;
            case TenAM: localizedOperatingHours = "10:00am"; break;
            case ElevenAM: localizedOperatingHours = "11:00am"; break;
            case TwelvePM: localizedOperatingHours = "12:00pm"; break;
            case OnePM: localizedOperatingHours = "1:00pm"; break;
            case TwoPM: localizedOperatingHours = "2:00pm"; break;
            case ThreePM: localizedOperatingHours = "3:00pm"; break;
            case FourPM: localizedOperatingHours = "4:00pm"; break;
            case FivePM: localizedOperatingHours = "5:00pm"; break;
            case SixPM: localizedOperatingHours = "6:00pm"; break;
            case SevenPM: localizedOperatingHours = "7:00pm"; break;
            case EightPM: localizedOperatingHours = "8:00pm"; break;
            case NinePM: localizedOperatingHours = "9:00pm"; break;
            case TenPM: localizedOperatingHours = "10:00pm"; break;
            case ElevenPM: localizedOperatingHours = "11:00pm" ; break;
            case TwelveAM: localizedOperatingHours = "12:00pm"; break;
            case OneAM: localizedOperatingHours = "1:00am"; break;
            case TwoAM: localizedOperatingHours = "2:00am"; break;
            case ThreeAM: localizedOperatingHours = "3:00am"; break;
            case FourAM: localizedOperatingHours = "4:00am"; break;
            case FiveAM: localizedOperatingHours = "5:00am"; break;
            case SixAM: localizedOperatingHours = "6:00am"; break;
            case SevenAM: localizedOperatingHours = "7:00am"; break;
            case OFF: localizedOperatingHours = "OFF DAY"; break;
         }
        return localizedOperatingHours;
    }
    
    // bahasa malaysia (ms_MY)
    private static String getOperatingHours_MS_MY(OperatingHours operatingHours) {
        String localizedOperatingHours = "";
        
        switch (operatingHours) {
            case EightAM: localizedOperatingHours = "8:00am" ; break;
            case NineAM: localizedOperatingHours = "9:00am"; break;
            case TenAM: localizedOperatingHours = "10:00am"; break;
            case ElevenAM: localizedOperatingHours = "11:00am"; break;
            case TwelvePM: localizedOperatingHours = "12:00pm"; break;
            case OnePM: localizedOperatingHours = "1:00pm"; break;
            case TwoPM: localizedOperatingHours = "2:00pm"; break;
            case ThreePM: localizedOperatingHours = "3:00pm"; break;
            case FourPM: localizedOperatingHours = "4:00pm"; break;
            case FivePM: localizedOperatingHours = "5:00pm"; break;
            case SixPM: localizedOperatingHours = "6:00pm"; break;
            case SevenPM: localizedOperatingHours = "7:00pm"; break;
            case EightPM: localizedOperatingHours = "8:00pm"; break;
            case NinePM: localizedOperatingHours = "9:00pm"; break;
            case TenPM: localizedOperatingHours = "10:00pm"; break;
            case ElevenPM: localizedOperatingHours = "11:00pm" ; break;
            case TwelveAM: localizedOperatingHours = "12:00pm"; break;
            case OneAM: localizedOperatingHours = "1:00am"; break;
            case TwoAM: localizedOperatingHours = "2:00am"; break;
            case ThreeAM: localizedOperatingHours = "3:00am"; break;
            case FourAM: localizedOperatingHours = "4:00am"; break;
            case FiveAM: localizedOperatingHours = "5:00am"; break;
            case SixAM: localizedOperatingHours = "6:00am"; break;
            case SevenAM: localizedOperatingHours = "7:00am"; break;
            case OFF: localizedOperatingHours = "OFF hari"; break;
         }
        return localizedOperatingHours;
    }
}